package com.paomi.client.activity.takeTicket;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.activity.takeTicket.model.TakeTicketBean;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.utils.RxRegUtils;
import com.paomi.client.view.WheelView;
import com.paomi.client.widget.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TakeTicketActivity extends BaseMvpActivity<MvpView, TakeTicketPresenter> {

    @BindView(R.id.take_name_et)
    EditText takeNameEt;
    @BindView(R.id.take_mobile_et)
    EditText takeMobileEt;
    @BindView(R.id.next_check)
    TextView nextCheck;
    @BindView(R.id.take_ide_et)
    EditText takeIdeEt;
    @BindView(R.id.btn_take)
    Button btnTake;
    @BindView(R.id.tv_cord)
    TextView tv_cord;
    Dialog cordDialog;//选择手机号码的区域
    @BindView(R.id.btn_delete)
    Button btnDelete;
    private List<String> list = new ArrayList<>();//选择区域的数据
    private TakeTicketBean takeTicketBean;

    @NonNull
    @Override
    public TakeTicketPresenter createPresenter() {
        return new TakeTicketPresenter();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.take_message);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_take_ticket;
    }

    @Override
    protected void initEvent() {
        nextCheck.setTypeface(iconfont);
        takeTicketBean = (TakeTicketBean) getIntent().getSerializableExtra("data");
        if (takeTicketBean != null) {
            btnDelete.setVisibility(View.VISIBLE);
            takeNameEt.setText(takeTicketBean.getName());
            takeMobileEt.setText(takeTicketBean.getMobile());
            takeIdeEt.setText(takeTicketBean.getCode());
        }
    }

    @OnClick({R.id.tv_cord, R.id.btn_take})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cord://获取
                list.clear();
                list.add("中国大陆 +86");
                list.add("香港 +852");
                list.add("澳门 +853");
                list.add("台湾 +886");
                initDialog();
                break;
            case R.id.btn_take:
                if (("").equals(takeNameEt.getText().toString().trim())) {
                    RxToast.info(getResources().getString(R.string.take_name) + getResources().getString(R.string.not_null));
                    return;
                }
                if (("").equals(takeMobileEt.getText().toString().trim())) {
                    RxToast.info(getResources().getString(R.string.phonenum) + getResources().getString(R.string.not_null));
                    return;
                }
                if (("").equals(takeIdeEt.getText().toString().trim())) {
                    RxToast.info(getResources().getString(R.string.id_number) + getResources().getString(R.string.not_null));
                    return;
                }
//                String name1 = takeNameEt.getText().toString().trim();
//                if (!RxRegUtils.isUsername(name1)) {
//                    RxToast.info(getResources().getString(R.string.take_name));
//                    return;
//                }
                String phone = takeMobileEt.getText().toString().trim();
                if (phone.length() < 6 || phone.length() > 12 || !RxRegUtils.isMobileExact(phone)) {
                    RxToast.info(getResources().getString(R.string.input_phoneNu_true));
                    return;
                }
                String iden = takeIdeEt.getText().toString().trim();
                if (iden.length() < 15 || iden.length() > 18 || !RxRegUtils.isIDCard(iden)) {
                    RxToast.info(getResources().getString(R.string.input_idcard_true));
                    return;
                }
                String name = takeNameEt.getText().toString().trim();
                String mobile = takeMobileEt.getText().toString().trim();
                String idNumber = takeIdeEt.getText().toString().trim();
                if (takeTicketBean != null) {
                    getPresenter().saveBtn("update", name, idNumber, "" + takeTicketBean.getId(), this);
                } else {
                    getPresenter().saveBtn("save", name, idNumber, "", this);
                }

                break;
        }
    }

    private void initDialog() {
        cordDialog = new Dialog(TakeTicketActivity.this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(TakeTicketActivity.this).inflate(R.layout.layout_choose_areacode, null);
        cordDialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        cordDialog.setCanceledOnTouchOutside(true);
        cordDialog.getWindow().setGravity(Gravity.BOTTOM);
        TextView success = cordDialog.findViewById(R.id.tv_success);
        TextView cancel = cordDialog.findViewById(R.id.tv_cancel);
        WheelView wheelView = cordDialog.findViewById(R.id.whellView);
        wheelView.setOffset(2);
        wheelView.setItems(list);
        wheelView.setSeletion(0);
        success.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String select = wheelView.getSeletedItem();
                int begin = select.indexOf("+");
                tv_cord.setText(select.substring(begin, select.length()));
                cordDialog.cancel();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cordDialog != null) {
                    cordDialog.cancel();
                }
            }
        });
        cordDialog.show();
    }

    @OnClick(R.id.btn_delete)
    public void onViewClicked() {
        getPresenter().deleteBtn(this,""+takeTicketBean.getId());
    }
}
